package com.shuosen.gmall.order.mq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shuosen.gmall.bean.enums.ProcessStatus;
import com.shuosen.gmall.service.OrderService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;

/***
 *
 */
@Component
public class OrderConsumer {

    @Reference
    private OrderService orderService  ;

    /**
     *
     * 1：从工厂中得到消息 使用注解
     * 2：判断消息分别处理业务
     */

    @JmsListener(destination = "PAYMENT_RESULT_QUEUE",containerFactory = "jmsQueueListener")
    public void consumerPaymentResult(MapMessage mapMessage) throws JMSException {
        String orderId = mapMessage.getString("orderId");
        String result = mapMessage.getString("result");
        //当返回的记录是scuccess就更新订单状态
        if("success".equals(result)){

            //更新订单状态
            orderService.updateOrderStatus(orderId, ProcessStatus.DELEVERED);
            // 通知减库存
            orderService.sendOrderStatus(orderId);
            //更新订单状态
            orderService.updateOrderStatus(orderId, ProcessStatus.NOTIFIED_WARE);
        }else {
            orderService.updateOrderStatus(orderId,ProcessStatus.UNPAID);
        }
    }

    @JmsListener(destination = "SKU_DEDUCT_QUEUE",containerFactory = "jmsQueueListener")
    public void consumeSkuDeduct(MapMessage mapMessage) throws JMSException {
        String orderId = mapMessage.getString("orderId");
        String  status = mapMessage.getString("status");
        if("DEDUCTED".equals(status)){
            orderService.updateOrderStatus(  orderId , ProcessStatus.WAITING_DELEVER);
        }else{
            orderService.updateOrderStatus(  orderId , ProcessStatus.STOCK_EXCEPTION);
        }
    }

}
